package com.jb.repair.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.jb.repair.R;
import com.jb.repair.acticity.CheckRegisActivity;
import com.jb.repair.model.DeviceModel;
import com.jb.repair.view.ListViewInScroll;

import java.util.List;

/**
 * Created by gaobin on 2015/11/2.
 */
public class CheckRegisParentAdapter extends BaseAdapter {

    private Context mContext;
    public List<DeviceModel> mData;
    private LayoutInflater mInflater;
    private Boolean isBatch;
    private boolean jumpFromTag;
    public static boolean IS_HISTORY = true; // 当前是否是历史任务

    private static final String TAG = "CheckRegisParentAdapter";

    public CheckRegisParentAdapter(Context mContext, List<DeviceModel> mData,Boolean isBatch, boolean jumpFromTag) {
        this.mContext = mContext;
        this.mData = mData;
        mInflater = LayoutInflater.from(mContext);
        this.isBatch = isBatch;
        this.jumpFromTag = jumpFromTag;
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null)
            convertView = mInflater.inflate(R.layout.item_check_info_parent, null);

        View ll_item_device_name = convertView.findViewById(R.id.ll_item_device_name);
        View ll_divider = convertView.findViewById(R.id.ll_divider);
        View ll_item_check_info_parent_title = convertView.findViewById(R.id.ll_item_check_info_parent_title);
        TextView tv_name = (TextView) convertView.findViewById(R.id.tv_name);
        TextView tv_pre_date = (TextView) convertView.findViewById(R.id.tv_pre_check_date);
        ListViewInScroll lv_today = (ListViewInScroll) convertView.findViewById(R.id.lv_today_check);
        ListViewInScroll lv_week = (ListViewInScroll) convertView.findViewById(R.id.lv_week_check_check);
        ListViewInScroll lv_month = (ListViewInScroll) convertView.findViewById(R.id.lv_month_check);
        ListViewInScroll lv_twicrweek = (ListViewInScroll) convertView.findViewById(R.id.lv_week_check_twice);
        ListViewInScroll lv_twicemonth = (ListViewInScroll) convertView.findViewById(R.id.lv_month_check_teice);
        ListViewInScroll lv_thirdweek = (ListViewInScroll) convertView.findViewById(R.id.lv_week_check_third);

        View ll_everyday = convertView.findViewById(R.id.ll_everyday);
        View ll_everyweek = convertView.findViewById(R.id.ll_everyweek);
        View ll_twiceweek = convertView.findViewById(R.id.ll_twiceweek);
        View ll_thirdweek = convertView.findViewById(R.id.ll_thirdweek);
        View ll_everymonth = convertView.findViewById(R.id.ll_everymonth);
        View ll_twicemonth = convertView.findViewById(R.id.ll_twicemonth);

        if(!CheckRegisActivity.changable){
            tv_pre_date.setVisibility(View.GONE);
        }

        DeviceModel model = mData.get(position);


        if (model.todayCheckList.size() == 0 && model.weekCheckList.size() == 0 && model.monthCheckList.size() == 0 && model.twiceWeekCheckList.size() == 0 && model.twiceMonthCheckList.size() == 0 && model.thirdWeekCheckList.size() == 0) {
            //如果该设备下没有任何点检项目，该设备不显示
//            Log.i(TAG, position+" 位置对应的 "+model.equipName+" 设备没有点检项目，不予显示");
            ll_item_device_name.setVisibility(View.GONE);
            ll_divider.setVisibility(View.GONE);
            ll_item_check_info_parent_title.setVisibility(View.GONE);
            ll_everyday.setVisibility(View.GONE);
            ll_everyweek.setVisibility(View.GONE);
            ll_everymonth.setVisibility(View.GONE);
            ll_twiceweek.setVisibility(View.GONE);
            ll_thirdweek.setVisibility(View.GONE);
            ll_twicemonth.setVisibility(View.GONE);
        } else {
            tv_name.setText(model.equipName);
//            Log.i(TAG,"设备名为：" + model.equipName);
            ll_item_device_name.setVisibility(View.VISIBLE);
            ll_divider.setVisibility(View.VISIBLE);
            ll_item_check_info_parent_title.setVisibility(View.VISIBLE);
            if (model.todayCheckList != null && model.todayCheckList.size() > 0) {
                ll_everyday.setVisibility(View.VISIBLE);
                CheckRegisChildAdapter todayAdapter = new CheckRegisChildAdapter(mContext, model.todayCheckList, mData.get(position).equipName, mData.get(position).id, position, 0, isBatch, jumpFromTag);
                lv_today.setAdapter(todayAdapter);
            } else {
                ll_everyday.setVisibility(View.GONE);
            }
            if (model.weekCheckList != null && model.weekCheckList.size() > 0) {
                ll_everyweek.setVisibility(View.VISIBLE);
                CheckRegisChildAdapter weekAdapter = new CheckRegisChildAdapter(mContext, model.weekCheckList, mData.get(position).equipName, mData.get(position).id, position, 1, isBatch, jumpFromTag);
                lv_week.setAdapter(weekAdapter);
            } else {
                ll_everyweek.setVisibility(View.GONE);
            }
            if (model.monthCheckList != null && model.monthCheckList.size() > 0) {
                ll_everymonth.setVisibility(View.VISIBLE);
                CheckRegisChildAdapter monthAdapter = new CheckRegisChildAdapter(mContext, model.monthCheckList, mData.get(position).equipName, mData.get(position).id, position, 2, isBatch, jumpFromTag);
                lv_month.setAdapter(monthAdapter);
            } else {
                ll_everymonth.setVisibility(View.GONE);
            }
            if (model.twiceWeekCheckList != null && model.twiceWeekCheckList.size() > 0) {
                ll_twiceweek.setVisibility(View.VISIBLE);
                CheckRegisChildAdapter twiceweekAdapter = new CheckRegisChildAdapter(mContext, model.twiceWeekCheckList, mData.get(position).equipName, mData.get(position).id, position, 3, isBatch, jumpFromTag);
                lv_twicrweek.setAdapter(twiceweekAdapter);
            } else {
                ll_twiceweek.setVisibility(View.GONE);
            }
            if (model.thirdWeekCheckList != null && model.thirdWeekCheckList.size() > 0) {
                ll_thirdweek.setVisibility(View.VISIBLE);
                CheckRegisChildAdapter thirdweekAdapter = new CheckRegisChildAdapter(mContext, model.thirdWeekCheckList, mData.get(position).equipName, mData.get(position).id, position, 4, isBatch, jumpFromTag);
                lv_thirdweek.setAdapter(thirdweekAdapter);
            } else {
                ll_thirdweek.setVisibility(View.GONE);
            }
            if (model.twiceMonthCheckList != null && model.twiceMonthCheckList.size() > 0) {
                ll_twicemonth.setVisibility(View.VISIBLE);
                CheckRegisChildAdapter twicemonthAdapter = new CheckRegisChildAdapter(mContext, model.twiceMonthCheckList, mData.get(position).equipName, mData.get(position).id, position, 5, isBatch, jumpFromTag);
                lv_twicemonth.setAdapter(twicemonthAdapter);
            } else {
                ll_twicemonth.setVisibility(View.GONE);
            }
        }

        return convertView;
    }

}

class ViewHolder {
    View ll_item_device_name;
    View ll_divider;
    View ll_item_check_info_parent_title;
    ListViewInScroll lv_today;
    ListViewInScroll lv_week;
    ListViewInScroll lv_month;
    ListViewInScroll lv_twicrweek;
    ListViewInScroll lv_twicemonth;
    ListViewInScroll lv_thirdweek;

    View ll_everyday;
    View ll_everyweek;
    View ll_twiceweek;
    View ll_thirdweek;
    View ll_everymonth;
    View ll_twicemonth;
}
